package com.tledu.email.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.tledu.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.tledu.common.annotation.Log;
import com.tledu.common.core.controller.BaseController;
import com.tledu.common.core.domain.AjaxResult;
import com.tledu.common.enums.BusinessType;
import com.tledu.email.domain.EmailTable;
import com.tledu.email.service.IEmailTableService;
import com.tledu.common.utils.poi.ExcelUtil;
import com.tledu.common.core.page.TableDataInfo;

/**
 * 邮箱Controller
 *
 * @author ruoyi
 * @date 2024-08-29
 */
@RestController
@RequestMapping("/email/inbox")
public class EmailTableController extends BaseController {
    @Autowired
    private IEmailTableService emailTableService;

    // 根据条件动态筛选用户收件箱的内容
    @PreAuthorize("@ss.hasPermi('email:inbox:selectByCondition')")
    @GetMapping("/condition")
    public TableDataInfo selectByCondition(EmailTable emailTable) {
        startPage();
        List<EmailTable> list = emailTableService.selectByCondition(emailTable);
        return getDataTable(list);
    }

    // 查询当前用户收件箱的内容
    // selectInboxContent
    @PreAuthorize("@ss.hasPermi('email:inbox:list')")
    @GetMapping("/inboxList")
    public TableDataInfo selectInboxList(EmailTable emailTable) {
        startPage();
        List<EmailTable> list = emailTableService.selectInboxList(emailTable);
        return getDataTable(list);
    }


    /**
     * 查询邮箱列表
     */
    @PreAuthorize("@ss.hasPermi('email:inbox:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmailTable emailTable) {
        startPage();
        List<EmailTable> list = emailTableService.selectEmailTableList(emailTable);
        return getDataTable(list);
    }

    /**
     * 导出邮箱列表
     */
    @PreAuthorize("@ss.hasPermi('email:inbox:export')")
    @Log(title = "邮箱", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmailTable emailTable) {
        List<EmailTable> list = emailTableService.selectEmailTableList(emailTable);
        ExcelUtil<EmailTable> util = new ExcelUtil<EmailTable>(EmailTable.class);
        util.exportExcel(response, list, "邮箱数据");
    }

    /**
     * 获取邮箱详细信息
     */
    @PreAuthorize("@ss.hasPermi('email:inbox:view')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(emailTableService.selectEmailTableById(id));
    }

    /**
     * 新增邮箱
     */
    @PreAuthorize("@ss.hasPermi('email:inbox:add')")
    @Log(title = "邮箱", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmailTable emailTable) {
        return toAjax(emailTableService.insertEmailTable(emailTable));
    }

    // 修改邮件状态未读为已读
    @PreAuthorize("@ss.hasPermi('email:inbox:changeStatus')")
    @Log(title = "修改邮件状态未读为已读", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus/{id}")
    public AjaxResult changeStatus(@PathVariable Long id) {
        // 调用服务层方法
        return toAjax(emailTableService.changeStatus(id));
    }

    /**
     * 修改邮箱
     */
    @PreAuthorize("@ss.hasPermi('email:inbox:edit')")
    @Log(title = "邮箱", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmailTable emailTable) {
        return toAjax(emailTableService.updateEmailTable(emailTable));
    }

    /*
    * 逻辑删除邮箱
    * */
    @PreAuthorize("@ss.hasPermi('email:inbox:edit')")
    @Log(title = "逻辑删除邮件", businessType = BusinessType.UPDATE)
    @PutMapping("/update/{ids}")
    public AjaxResult updateSymbol(@PathVariable Long[] ids) {
        return toAjax(emailTableService.updateSymbol(ids));
    }

    /**
     * 删除邮箱
     */
    @PreAuthorize("@ss.hasPermi('email:inbox:remove')")
    @Log(title = "邮箱", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(emailTableService.deleteEmailTableByIds(ids));
    }
}
